package com.weishui.talking.listener;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import com.weishui.talking.app.TalkingWindow;
import com.weishui.talking.socket.ClientSocket;


public class ButtonActionListener implements ActionListener {

	private TalkingWindow talkingWindow;
	public ButtonActionListener(TalkingWindow talkingWindow){
		this.talkingWindow = talkingWindow;
	}
	
	private String serverIp = null;
	private String contentMessage = null;
	
	@Override
	public void actionPerformed(ActionEvent event) {
		// TODO Auto-generated method stub
		if("clear".equals(event.getActionCommand())){
			talkingWindow.textAreaMessage.setText("");
		}else if("send".equals(event.getActionCommand())){
			serverIp = talkingWindow.jIpAddressField.getText();
			contentMessage = talkingWindow.textAreaMessage.getText().trim();
			if("0.0.0.0".equals(serverIp) || "".equals(serverIp) || serverIp == null){
				Toolkit.getDefaultToolkit().beep();
				JOptionPane.showMessageDialog(null, "请输入IP地址!", "ERROR_MESSAGE",JOptionPane.ERROR_MESSAGE);
				return;
			}
			if("".equals(contentMessage) || contentMessage == null){
				Toolkit.getDefaultToolkit().beep();
				JOptionPane.showMessageDialog(null, "请输入信息后再发送!", "ERROR_MESSAGE",JOptionPane.ERROR_MESSAGE);
				return;
			}
			if(!ClientSocket.sendMessage(serverIp, contentMessage)){
				Toolkit.getDefaultToolkit().beep();
				JOptionPane.showMessageDialog(null, "发送消息失败!", "ERROR_MESSAGE",JOptionPane.ERROR_MESSAGE);
				return;
			}
			talkingWindow.textAreaMessage.setText("");
		}
	}

}
